package com.howie.parnote.service.common;

import com.howie.parnote.util.GenericsUtil;
import com.howie.parnote.util.LogUtil;
import com.howie.parnote.util.StringUtil;
import com.howie.parnote.dao.common.ApplicationBaseDaoIntf;
import com.howie.parnote.util.spring.SpringContextUtil;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

/**
 * Created by HY on 2014/12/8 0008.
 */
public abstract class AbstractService<T> implements ApplicationBaseServiceIntf<T> {

	protected Logger logger = LogUtil.getLogger(this.getClass());

	private Class<T> clazz;

	public AbstractService() {
		Class<T> entityClass = GenericsUtil.getSuperClassGenricType(getClass());
		this.clazz = entityClass;
	}

	private static final ApplicationContext daoCtx = SpringContextUtil.getApplicationContext();

	private static final String MAPPER_SUFFIX = "Mapper";

	protected <T extends ApplicationBaseDaoIntf> T getDao() {
		String fullClazzName = getClazz().getName();
		return (T) daoCtx.getBean(StringUtil.replaceFirstToLower(fullClazzName.substring(fullClazzName.lastIndexOf(".") + 1, fullClazzName.length())) + MAPPER_SUFFIX);
	}

	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}
}
